package com.yuke.cloud.service.omc.payUtil;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.AMQP;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Getbanknameutil {
    public static String getbankname(String code) {
        String url = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=";
        url += code;
        url += "&cardBinCheck=true";
        //经常用，不安全
        StringBuilder sb = new StringBuilder();
        try {
            URL urlObject = new URL(url);
            URLConnection urlConnection = null;
            try {
                urlConnection = urlObject.openConnection();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                BufferedReader bf = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                String inputline = null;
                while ((inputline = bf.readLine()) != null) {
                    sb.append(inputline);
                }
                bf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }


        return getbankcode(sb.toString());
    }

    public static String getbankcode(String bank){
        JSONObject jsonObject = JSONObject.parseObject(bank);
        String bank1 = jsonObject.get("bank").toString();
        if (bank1.equals("ICBC")){
            return "1002";
        }
        if (bank1.equals("ABC")){
            return "1005";
        }
        if (bank1.equals("BOC")){
            return "1026";
        }
        if (bank1.equals("CCB")){
            return "1003";
        }
        if (bank1.equals("CMB")){
            return "1001";
        }
        if (bank1.equals("PSBC")){
            return "1066";
        }
        if (bank1.equals("BOCOM")){
            return "1020";
        }
        if (bank1.equals("SPDB")){
            return "1004";
        }
        if (bank1.equals("CMBC")){
            return "1006";
        }
        if (bank1.equals("CIB")){
            return "1009";
        }
        if (bank1.equals("PINGAN")){
            return "1010";
        }
        if (bank1.equals("CCITICB")){
            return "1021";
        }
        if (bank1.equals("HXB")){
            return "1025";
        }
        if (bank1.equals("CGBC")){
            return "1027";
        }
        if (bank1.equals("CEBB")){
            return "1022";
        }
        if (bank1.equals("BOB")){
            return "1032";
        }
        if (bank1.equals("NBCB")){
            return "1056";
        }
        return "不支持的银行卡";
    }
}
